package com.aistar.controller;

import com.aistar.service.JobService;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
public class JobController {
    @Autowired
    private JobService jobService;

    @GetMapping("/job/latest")
    @ResponseBody
    public ServerResponse getLatest(){
        ServerResponse serverResponse = jobService.getSomeLatest();
        return serverResponse;
    }

    @GetMapping("/job/all")
    @ResponseBody
    public ServerResponse getAll(String kw, String city, String publishtimeStr, Float minsalary,
                                 Float maxsalary, String workyear, String degree, String company, Integer page){
        //(1)判断 查询关键字
        if(kw==null || kw.trim().equals(""))
            kw = null;
        //(2) 判断城市
        if(city==null || city.trim().equals(""))
            city = null;

        //(3)发布日期
        Date publishtime = null;
        if(publishtimeStr!=null && !publishtimeStr.equals(""))
            publishtime = DateUtil.getBeforeDate(Integer.valueOf(publishtimeStr));

        //(4) 最低薪资 最高薪资


        if(minsalary==null && minsalary.equals(""))
            minsalary =0f;

        if(maxsalary==null && maxsalary.equals(""))
            maxsalary =50000f;

        //(5)学历
        if(degree ==null || degree.trim().equals(""))
            degree =null;
        //(6)工作年限
        if(workyear ==null || workyear.trim().equals(""))
            workyear =null;

        //(7)页码

        if(page == null)
            page =1;


        ServerResponse serverResponse = jobService.getAll(kw,city,publishtime,minsalary,maxsalary,workyear,degree,company,page);

        //4. 返回ServerResponse (data: pageInfo<JobCompanyInfo>)
        return serverResponse;
    }
    @GetMapping("/job/{jobId}")
    @ResponseBody
    public ModelAndView detail(@PathVariable("jobId") Integer jobId){
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = this.jobService.getById(jobId);
        mav.addObject("jobCompany",serverResponse.getData());
        mav.setViewName("/job/job_detail");
        return mav;
    }
}
